package com.beagle.njddapp.ui.fragment;

import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseMvpFragment;
import com.beagle.njddapp.bean.owner.DIrverOrderBean;
import com.beagle.njddapp.ui.activity.owner.NjDirverOrderDetailActivity;
import com.beagle.njddapp.ui.adapter.NjDirverListAdapter;
import com.beagle.njddapp.ui.adapter.NjOrderDirverListAdapter;
import com.beagle.njddapp.ui.adapter.inter.RecyclerViewItemClickLisenter;
import com.beagle.njddapp.ui.presenter.FindDirverPresenter;
import com.beagle.njddapp.ui.view.FindDirverView;
import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author hxx
 * @date 2021-03-15 19:29
 * 农机驾驶员订单
 */
public class DIrverOrderFragment extends BaseMvpFragment<FindDirverView, FindDirverPresenter> implements FindDirverView, RecyclerViewItemClickLisenter<DIrverOrderBean>, NjOrderDirverListAdapter.DirverOrderCzCallBack {

    @BindView(R.id.common_recyclerview)
    RecyclerView recyclerView;
    private NjOrderDirverListAdapter njDirverListAdapter;
    private List<DIrverOrderBean> orderBeanList;
    @BindView(R.id.ll_empty)
    View empty;




    @Override
    public int getLayoutId() {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public void init() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        orderBeanList=new ArrayList<>();
        njDirverListAdapter=new NjOrderDirverListAdapter(orderBeanList,getContext());
        njDirverListAdapter.setDirverOrderCzCallBack(this);
        njDirverListAdapter.setOnItemClickLisenter(this);
        recyclerView.setAdapter(njDirverListAdapter);
        presenter.getList(1000,1);
    }

    @Override
    public FindDirverView getMvpView() {
        return this;
    }

    @Override
    public FindDirverPresenter createPresenter() {
        return new FindDirverPresenter();
    }

    @Override
    public void onError(String message) {
        ToastUtils.showShort(message);
    }


    @Override
    public void showList(List<DIrverOrderBean> results) {
        if (results==null||results.size()<1){
            empty.setVisibility(View.VISIBLE);
            return;
        }
        empty.setVisibility(View.GONE);
        orderBeanList.addAll(results);
        njDirverListAdapter.notifyDataSetChanged();
    }

    @Override
    public void updateSuccess() {
        ToastUtils.showShort("移交成功，待驾驶员确认");
        presenter.getList(1000,1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==306){
            presenter.getList(1000,1);
        }
    }

    @Override
    public void onItemClickLisenter(DIrverOrderBean dIrverOrderBean) {
        startActivityForResult(new Intent(getContext(), NjDirverOrderDetailActivity.class).putExtra("data",dIrverOrderBean),307);
    }

    @Override
    public void caozuo(DIrverOrderBean dIrverOrderBean) {
        if (2==dIrverOrderBean.getOrderStatus()){
            dIrverOrderBean.setOrderStatus(3);
            presenter.updateFindDriverOrder(dIrverOrderBean);
        }
//        else if (4==dIrverOrderBean.getOrderStatus()){
//            dIrverOrderBean.setOrderStatus(5);
//            presenter.updateFindDriverOrder(dIrverOrderBean);
//        }
    }
}
